package tool

import (
	"fmt"
	"math/rand"
	"strconv"
	"strings"
	"time"
)

func TimeFormat(ts int64, layout string) string {
	if ts == 0 {
		return "-"
	}

	if layout == "" {
		layout = "2006-01-02 15:04:05"
	}
	t := time.Unix(ts, 0)
	if t.IsZero() {
		return "-"
	}
	return t.Format(layout)
}
func GenerateOrderId(merchantId string) string {
	now := time.Now()
	timestamp := now.Format("20060102150405") // 日期时间戳
	msec := now.Nanosecond() / 1000000        // 毫秒数

	randInt := rand.Intn(900) + 100 // 3位随机数

	orderId := fmt.Sprintf("%s%s%03d%03d", merchantId, timestamp, msec, randInt)
	return orderId
}

func ConvertInt32SliceToString(data []int32) string {
	var strBuilder strings.Builder
	for i, v := range data {
		if i > 0 {
			strBuilder.WriteString(", ")
		}
		strBuilder.WriteString(strconv.Itoa(int(v)))
	}
	return strBuilder.String()
}
